﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToRTR.Views.CUiExportToRTRContentTreeView
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.ExportToRTR.Resources;
using DnProcessSimulateCommands.InterferenceVolumeQuery.Tree;
using System;
using System.CodeDom.Compiler;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Markup;
using System.Windows.Threading;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;


namespace DnProcessSimulateCommands.ExportToRTR.Views;

public class CUiExportToRTRContentTreeView : System.Windows.Controls.UserControl, INotifyPropertyChanged, IComponentConnector
{
  private ExportToRTRTreeListModel dataModel;
  private TxObjectList objectsInTree;
  internal CUiExportToRTRContentTreeView ExportToRTRTreeView;
  internal System.Windows.Controls.Button addButton;
  internal System.Windows.Controls.Button removeButton;
  internal TreeList tree;
  private bool _contentLoaded;

  public CUiExportToRTRContentTreeView()
  {
    this.InitializeComponent();
    this.dataModel = new ExportToRTRTreeListModel();
    this.objectsInTree = new TxObjectList();
    this.tree.Model = (ITreeModel) this.dataModel;
    TxApplication.ActiveDocument.Selection.ItemsAdded += new TxSelection_ItemsAddedEventHandler(this.Selection_ItemsAdded);
    TxApplication.ActiveDocument.Selection.ItemsRemoved += new TxSelection_ItemsRemovedEventHandler(this.Selection_ItemsRemoved);
    TxApplication.ActiveDocument.Selection.ItemsSet += new TxSelection_ItemsSetEventHandler(this.Selection_ItemsSet);
    TxApplication.ActiveDocument.Selection.Cleared += new TxSelection_ClearedEventHandler(this.Selection_Cleared);
    this.tree.SelectionChanged += new SelectionChangedEventHandler(this.tree_SelectionChanged);
    this.Unloaded += new RoutedEventHandler(this.CUiExportToRTRContentTreeView_Unloaded);
    TxApplication.ActiveUndoManager.StartTransaction();
  }

  public event PropertyChangedEventHandler PropertyChanged;

  private void SetProperty<T>(ref T valueToSet, T value, string propertyName)
  {
    valueToSet = value;
    this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
  }

  private void OnPropertyChanged(PropertyChangedEventArgs e)
  {
    PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
    if (propertyChanged == null)
      return;
    propertyChanged((object) this, e);
  }

  private IExportToRTRTreeViewItem RootNode
  {
    get
    {
      DnProcessSimulateCommands.InterferenceVolumeQuery.Tree.TreeNode treeNode = this.tree.SelectedNode;
      if (treeNode == null)
        return (IExportToRTRTreeViewItem) null;
      while (treeNode.Parent.Parent != null)
        treeNode = treeNode.Parent;
      return treeNode.Tag as IExportToRTRTreeViewItem;
    }
  }

  private void AddSelectedObjectsInTree()
  {
    this.addButton.IsEnabled = false;
    Cursor current = Cursor.Current;
    Cursor.Current = Cursors.WaitCursor;
    TxObjectList selection = TxApplication.ActiveDocument.Selection.GetFilteredItems(CUiExportToRTRContentViewModel.GetSupportedOperationFilter());
    CUiExportToRTRContentViewModel model = this.DataContext as CUiExportToRTRContentViewModel;
    if (model != null)
    {
      TxObjectList operationsFromSelection = CUiExportToRTRContentDataHandler.ComputeOperationsFromSelection<ITxRoboticOrderedCompoundOperation>(selection);
      TxObjectList txObjectList = new TxObjectList(this.objectsInTree);
      foreach (ITxObject itxObject in (Collection<ITxObject>) this.objectsInTree)
      {
        if (((Collection<ITxObject>) operationsFromSelection).Contains(itxObject))
          ((Collection<ITxObject>) operationsFromSelection).Remove(itxObject);
      }
      ((TxObjectList<ITxObject>) txObjectList).AddRange((TxObjectList<ITxObject>) selection);
      this.UpdateLocationsList(operationsFromSelection);
      if (((Collection<ITxObject>) selection).Count > 0)
        model.CurrentDispatcher.BeginInvoke((Delegate) (() =>
        {
          foreach (IExportToRTRTreeViewItem treeViewItem in CApExportToRTRManager.CreateTreeViewItems(selection, model))
          {
            if (!((Collection<ITxObject>) this.objectsInTree).Contains(treeViewItem.ExportToRTRObject))
            {
              (this.dataModel.RootNode as IExportToRTRTreeViewItem).Children.Add(treeViewItem);
              ((Collection<ITxObject>) this.objectsInTree).Add(treeViewItem.ExportToRTRObject);
            }
          }
        }), DispatcherPriority.Background);
    }
    Cursor.Current = current;
  }

  private void tree_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
    DnProcessSimulateCommands.InterferenceVolumeQuery.Tree.TreeNode selectedNode = this.tree.SelectedNode;
    ITxObject exportToRtrObject = selectedNode?.Tag is IExportToRTRTreeViewItem tag ? tag.ExportToRTRObject : (ITxObject) null;
    this.removeButton.IsEnabled = exportToRtrObject != null && !(exportToRtrObject is ITxLocationOperation);
    if (selectedNode == null)
      return;
    CApExportToRTRManager.ChangeSelection(selectedNode);
  }

  private void EnableDisableButtons()
  {
    this.addButton.IsEnabled = false;
    TxSelection selection = TxApplication.ActiveDocument.Selection;
    if (selection != null && selection.Count > 0)
    {
      TxObjectList filteredItems = selection.GetFilteredItems(CUiExportToRTRContentViewModel.GetSupportedOperationFilter());
      Type[] typeArray1 = new Type[1]
      {
        typeof (ITxRoboticLocationOperation)
      };
      TxTypeFilter txTypeFilter = new TxTypeFilter(typeArray1);
      if (!TxFeatureManager.CheckFeature("RTR_EXPORT_MORE_TYPES"))
      {
        Type[] typeArray2 = new Type[1]
        {
          typeof (TxRoboticSeamLocationOperation)
        };
        txTypeFilter = new TxTypeFilter(typeArray1, typeArray2);
      }
      foreach (ITxObject itxObject in (Collection<ITxObject>) filteredItems)
      {
        if (itxObject is ITxObjectCollection)
        {
          int? count = ((Collection<ITxObject>) (itxObject as ITxObjectCollection).GetAllDescendants((ITxTypeFilter) txTypeFilter))?.Count;
          int num = 0;
          if (count.GetValueOrDefault() > num & count.HasValue)
          {
            this.addButton.IsEnabled = true;
            break;
          }
        }
      }
    }
    else
      this.addButton.IsEnabled = false;
  }

  private void Selection_Cleared(object sender, TxSelection_ClearedEventArgs args)
  {
    this.addButton.IsEnabled = false;
  }

  private void Selection_ItemsSet(object sender, TxSelection_ItemsSetEventArgs args)
  {
    this.EnableDisableButtons();
  }

  private void Selection_ItemsRemoved(object sender, TxSelection_ItemsRemovedEventArgs args)
  {
    this.EnableDisableButtons();
  }

  private void Selection_ItemsAdded(object sender, TxSelection_ItemsAddedEventArgs args)
  {
    this.EnableDisableButtons();
  }

  private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
  {
    this.AddSelectedObjectsInTree();
  }

  private void CUiExportToRTRContentTreeView_Unloaded(object sender, RoutedEventArgs e)
  {
    TxApplication.ActiveDocument.Selection.ItemsAdded -= new TxSelection_ItemsAddedEventHandler(this.Selection_ItemsAdded);
    TxApplication.ActiveDocument.Selection.ItemsRemoved -= new TxSelection_ItemsRemovedEventHandler(this.Selection_ItemsRemoved);
    TxApplication.ActiveDocument.Selection.ItemsSet -= new TxSelection_ItemsSetEventHandler(this.Selection_ItemsSet);
    TxApplication.ActiveDocument.Selection.Cleared -= new TxSelection_ClearedEventHandler(this.Selection_Cleared);
    TxApplication.ActiveUndoManager.EndTransaction();
  }

  private void removeButton_Click(object sender, RoutedEventArgs e)
  {
    if (this.tree.SelectedNode == null)
      return;
    IExportToRTRTreeViewItem rootNode = this.RootNode;
    ((IExportToRTRTreeViewItem) this.tree.GetModelRootNode).Children.Remove(rootNode);
    ((Collection<ITxObject>) this.objectsInTree).Remove(rootNode.ExportToRTRObject);
    if (!(this.DataContext is CUiExportToRTRContentViewModel dataContext))
      return;
    TxObjectList operations = new TxObjectList();
    ((Collection<ITxObject>) operations).Add(rootNode.ExportToRTRObject);
    dataContext.RemoveOperationLocations(operations);
  }

  private void UpdateLocationsList() => this.UpdateLocationsList(this.objectsInTree);

  private void UpdateLocationsList(TxObjectList objectToUpdate)
  {
    if (!(this.DataContext is CUiExportToRTRContentViewModel dataContext))
      return;
    dataContext.UpdateLocationsList(objectToUpdate);
  }

  private void addButton_Click(object sender, RoutedEventArgs e)
  {
    this.addButton.IsEnabled = false;
    this.AddSelectedObjectsInTree();
  }

  private void ComboBox_RoadMapConnection_SelectionChanged(
    object sender,
    SelectionChangedEventArgs e)
  {
    if (!(sender is System.Windows.Controls.ComboBox comboBox) || !(comboBox.DataContext is ExportToRTRTreeItem dataContext))
      return;
    string addedItem = e.AddedItems[0] as string;
    if (addedItem == ExportToRTRStringTable.TREE_ROADMAP_CONNECTION_CONNECTED_TO_ALL)
    {
      if (dataContext.ExportToRTRLocationData.ConnectionType == CApExportToRTRLocationData.ExportToRTRLocationDataConnectionType.CONNECTION_TYPE_TO_ALL)
        return;
      dataContext.ExportToRTRLocationData.ConnectionType = CApExportToRTRLocationData.ExportToRTRLocationDataConnectionType.CONNECTION_TYPE_TO_ALL;
    }
    else
    {
      if (!(addedItem == ExportToRTRStringTable.TREE_ROADMAP_CONNECTION_CONNECTED_TO_NEXT) || dataContext.ExportToRTRLocationData.ConnectionType == CApExportToRTRLocationData.ExportToRTRLocationDataConnectionType.CONNECTION_TYPE_TO_NEXT)
        return;
      dataContext.ExportToRTRLocationData.ConnectionType = CApExportToRTRLocationData.ExportToRTRLocationDataConnectionType.CONNECTION_TYPE_TO_NEXT;
    }
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    System.Windows.Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/exporttortr/window/views/cuiexporttortrcontenttreeview.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  internal Delegate _CreateDelegate(Type delegateType, string handler)
  {
    return Delegate.CreateDelegate(delegateType, (object) this, handler);
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        this.ExportToRTRTreeView = (CUiExportToRTRContentTreeView) target;
        this.ExportToRTRTreeView.DataContextChanged += new DependencyPropertyChangedEventHandler(this.UserControl_DataContextChanged);
        break;
      case 2:
        this.addButton = (System.Windows.Controls.Button) target;
        this.addButton.Click += new RoutedEventHandler(this.addButton_Click);
        break;
      case 3:
        this.removeButton = (System.Windows.Controls.Button) target;
        this.removeButton.Click += new RoutedEventHandler(this.removeButton_Click);
        break;
      case 4:
        this.tree = (TreeList) target;
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
